package com.isoft.dao.impl;

import com.isoft.pojo.Person;
import com.isoft.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddressBookDAOImpl implements IAddressBookDAO {

    @Override
    public int insert(Person person) {
        //核心代码
        String sql = "insert into tb_user values(null,?,?,?,?)";
        List<Object> list = new ArrayList();
        list.add(person.getName());
        list.add(person.getGender());
        list.add(person.getPosition());
        list.add(person.getTelephone());
        int i = DBUtils.executeUpdate(sql, list);
        return i;
    }

    @Override
    public Person findByName(String name) {
        String sql = "select * from tb_user where name=?";
        List list = new ArrayList();
        list.add(name);
        List<Person> list1 = DBUtils.executeQuery(sql, list, Person.class);
       if (list1.size()==0){
           return null;
       }
        return list1.get(0);
    }

    @Override
    public int deleteByName(String name) {
        String sql="delete from tb_user where name=?";
        List<Object> list = new ArrayList();
        list.add(name);
        int i = DBUtils.executeUpdate(sql, list);
        return i;
    }

    @Override
    public int updateByName(Person person) {
        String sql="update tb_user set name=?,gender=?,position=?,telephone=? where name=?";
        List<Object> list = new ArrayList();
        list.add(person.getName());
        list.add(person.getGender());
        list.add(person.getPosition());
        list.add(person.getTelephone());
        list.add(person.getName());
        int i = DBUtils.executeUpdate(sql, list);//影响结果的记录数
        return i;
    }
}
